import java.util.Scanner;

/**
 * @Package: PACKAGE_NAME
 * @ClassName: TwoNumAdd
 * @Author: HuangChengLong
 * @CreateTime: 2022/3/14 10:43
 * @Description: 两数相加
 */

public class TwoNumAdd {
    public static void main(String[] args) {
        //两个链表
        MyList mylist1 = new MyList();
        MyList mylist2 = new MyList();
        //存放输出
        MyList mylist3 = new MyList();

        Scanner scanner = new Scanner(System.in);
        //String in=scanner.next();
        String in="l1 = [9，9，9，9，9，9，9],l2=[9，9，9，9]";


        //对输入的字符串进行处理，分别导入两个逆序链表
        //导入第一个链表
        int interval=0;
        for (int i = 4; i < in.length(); i++) {
            char c=in.charAt(i);
            if (Character.isDigit(c)){
                //System.out.println(c+"--");
                mylist1.insert(new Node(Integer.parseInt(String.valueOf(c))));
            }
            if (c==']'){
                interval=i+6;
                break;
            }
        }

         //测试输出第一个链表
//        System.out.println("myList1.size="+mylist1.getSize());
//        Node temp=mylist1.getHead();
//        while (temp!=null){
//            System.out.println(temp.val);
//            temp=temp.next;
//        }

        //导入第二个链表
        for (int i = interval; i < in.length(); i++) {
            char c=in.charAt(i);
            if (Character.isDigit(c)){
                mylist2.insert(new Node(Integer.parseInt(String.valueOf(c))));
                //System.out.println(c);
            }
        }

        //链表相加
        int carry=0;//进位
        Node temp1=mylist1.getHead();
        Node temp2=mylist2.getHead();

        //判断使用长度的链表作为循环变量
        boolean loop=false;
        if (mylist1.getSize()>=mylist2.getSize()){
            loop=true;
        }
        while ((loop?temp1:temp2)!=null){
            int n1=0;
            int n2=0;
            //如果为空就等于0，链表不在后移，避免空指针异常
            if (temp1==null){
                n1=0;
            }
            else {
                n1=temp1.val;
                temp1=temp1.next;
            }
            //如果为空就等于0，链表不在后移，避免空指针异常
            if (temp2==null){
                n2=0;
            }
            else {
                n2=temp2.val;
                temp2=temp2.next;
            }

            //当前位求和
            int sum=n1+n2+carry;
            //System.out.println("sum("+sum+")"+"="+"n1("+n1+")"+"n2("+n2+")"+"carry("+carry+")");
            //当前位求进位
            carry=sum/10;
            //将当前位作为节点添加到myList3链表
            mylist3.insert(new Node(sum%10));

        }


        if (carry!=0) {
            mylist3.insert(new Node(carry));
        }

        System.out.println(in);
        //System.out.println("myList3.size="+mylist3.getSize());
        Node temp=mylist3.getHead();
        System.out.print("[");
        while (temp.next!=null){
            System.out.print(temp.val+",");
            temp=temp.next;
        }
        System.out.print(temp.val);
        System.out.print("]");

    }
}
//节点类
class Node{
    public int val;
    public Node next;

    public Node() {
    }

    public Node(int n){
        this.val=n;
        next=null;
    }
}

//链表类
class MyList{
    private Node head;
    private int size = 0;

    public MyList() {
    }

    //插入;
    public boolean insert(Node newNode){
        if(head == null){
            head = newNode;
            //System.out.println(head.val+"insert");
            size++;
            return true;
        }
        Node temp = head;
        while(temp.next != null){
            temp = temp.next;
        }
        temp.next = newNode;
        //System.out.println(temp.next.val+"insert");
        size++;
        return true;
    }

    //返回链表长度
    public int getSize() {
        return size;
    }
    //返回头节点
    public Node getHead() {
        return head;
    }
}
